<template>

	<view class="page-container">

		<view class="userinfo-avatar-container">
			<image :src="userInfo.avatar" class="userinfo-avatar" @click='previewAvatar'></image>
			<image src="/static/image/public/Camera.png" class="userinfo-mobile-icon" @click='changeAvatar'></image>
		</view>
		<view class="infomation-container">
			<view class="infomation-item-container">
				<view class="infomation-item-label-container">
					<image src="/static/image/public/NickName.png" class="infomation-item-label-icon"></image>
					<text class="infomation-item-label-text">用户昵称</text>
				</view>
				<view class="infomation-item-value-container" @click='changeNickName'>
					<text class="infomation-item-value">{{userInfo.nickname}}</text>
					<image src="/static/image/public/back.png" class="infomation-item-value-jump-icon"></image>
				</view>
			</view>
			<view class="infomation-item-container">
				<view class="infomation-item-label-container">
					<image src="/static/image/public/Mobile.png" class="infomation-item-label-icon"></image>
					<text class="infomation-item-label-text">我的手机</text>
				</view>
				<view class="infomation-item-value-container" @click="changePhotoNumber">
					<text class="infomation-item-value">{{userInfo.mobile}}</text>
					<image src="/static/image/public/back.png" class="infomation-item-value-jump-icon"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script lang='ts' setup>
	import {
		ref
	} from 'vue';
	import {
		uploadImage,
		useShowToast
	} from "@/utils/tools";
	import {
		onShow
	} from '@dcloudio/uni-app';
	import {
		apiUpdateAvatar
	} from "@/api/user";

	const userInfo = ref({
		avatar: "",
		nickname: "",
		mobile: "",
	})

	onShow(() => {
		console.log('更新用户信息')
		updateUserInfoByStorage();
	})

	/**
	 * 从缓存更新最新的用户信息
	 */
	const updateUserInfoByStorage = () => {
		const storageInfo = uni.getStorageSync('userInfo');
		userInfo.value = storageInfo;
	}
	
	const uploadAvatar = async (filePath: string) => {
		let avatarURL = '';
		uni.showLoading({
			mask: true,
			title: "图片上传中"
		})
		try {
			const response = await uploadImage(filePath)
			avatarURL = response.data.url;
		} catch (err) {
			uni.hideLoading();
			useShowToast('图片上传失败，请稍后重试')
			return
		}
		try {
			await apiUpdateAvatar(avatarURL);
			const userInfo = uni.getStorageSync("userInfo");
			userInfo.avatar = avatarURL;
			uni.setStorageSync("userInfo", userInfo);
			updateUserInfoByStorage();
		} catch (err) {
			uni.hideLoading();
			useShowToast('图片保存失败')
			return
		}
		uni.hideLoading();
	}
	/**
	 * 选择头像
	 */
	const changeAvatar = () => {
		uni.chooseImage({
			count: 1,
			success: async (res) => {
				uploadAvatar(res.tempFilePaths[0]);
			},
			fail: (err) => {
				console.error(err);
				useShowToast('获取图片失败')
			}
		})
	}



	/**
	 * 预览头像
	 */
	const previewAvatar = () => {
		const list = userInfo.value.avatar
		uni.previewImage({
			urls: [list]
		})
	}

	/**
	 * 改变昵称
	 */
	const changeNickName = () => {
		uni.navigateTo({
			url: "/pages/modify-info-nickname/index"
		})
	}

	/**
	 * 改变手机号
	 */

	const changePhotoNumber = () => {
		uni.navigateTo({
			url: "/pages/modify-info-photo/index"
		})
	}
</script>

<style lang="scss" scoped>
	.page-container {
		.userinfo-avatar-container {
			width: 208rpx;
			height: 208rpx;
			position: relative;
			margin: 0 auto;
			padding-top: 56rpx;
			padding-bottom: 94rpx;

			.userinfo-avatar {
				width: 208rpx;
				height: 208rpx;
				border-radius: 50%;
				overflow: hidden;
			}

			.userinfo-mobile-icon {
				position: absolute;
				width: 64rpx;
				height: 64rpx;
				bottom: 94rpx;
				right: -8rpx;
			}
		}

		.infomation-container {
			width: 100%;
			box-sizing: border-box;
			padding: 0 60rpx;

			.infomation-item-container {
				width: 100%;
				height: 90rpx;
				border-bottom: 1rpx solid #4A4A4A;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.infomation-item-label-container {
					display: flex;
					align-items: center;

					.infomation-item-label-icon {
						width: 40rpx;
						height: 40rpx;
						margin-right: 16rpx;
					}

					.infomation-item-label-text {
						font-size: 28rpx;
						color: #FFFFFF;
						line-height: 28rpx;
					}
				}

				.infomation-item-value-container {
					width: 420rpx;
					display: flex;
					justify-content: flex-end;
					align-items: center;

					.infomation-item-value-jump-icon {
						width: 28rpx;
						height: 28rpx;
						margin-left: 20rpx;
					}

					.infomation-item-value {
						font-size: 28rpx;
						color: #8A8A8A;
						text-overflow: ellipsis;
						overflow: hidden;
						word-break: break-all;
						white-space: nowrap;
					}
				}
			}
		}
	}
</style>